import { makeAutoObservable } from 'mobx'
import type { levelListType } from './interface'
import { getLevelList, submitTraining, userBarriersIsFinishScene } from './api'
import { Toast } from 'antd-mobile'
class Store {
    levelList: levelListType[] = [] //关卡列表
    isSubmitTraining: boolean = false
    currentIdx: number = 0
    isSubmitValue: string = ''

    constructor() {
        makeAutoObservable(this)
    }
    //获取关卡列表
    async getLevelListData(id: string) {
        await this.getIsSubmitTraining(id)
        const { data } = await getLevelList(id)
        this.levelList = data as levelListType[]
        document.title = data?.[0]?.barrierScene?.name
        const idx = data.findIndex((item: levelListType) => item.is_player === '0')
        this.currentIdx = idx > -1 ? idx : 0
    }
    //提交实训
    async putTraining(id: string) {
        await submitTraining(id)
            .then(() => {
                Toast.show({
                    icon: 'success',
                    content: '提交成功',
                })
                window.wx.miniProgram.reLaunch({ url: '/pages/scene/index' })
            })
            .catch(() => {
                Toast.show({
                    icon: 'fail',
                    content: '提交失败',
                })
            })
    }
    // 获取是否提交过实训
    async getIsSubmitTraining(id: string) {
        await userBarriersIsFinishScene(id).then(({ data }) => {
            this.isSubmitTraining = data.is_finish === '1'
            this.isSubmitValue = data.is_finish
        })
    }
}
export default Store
